package com.dhcc.utils.string;

import com.dhcc.utils.Operator;
import com.dhcc.utils.OperatorException;

/**
 * support afterX "head"
 * @author hibean@qq.com 2017年6月28日
 *
 */
public class AfterOperator implements Operator<String> {

	@Override
	public Object invoke(String operator, String first, Object[] args) {
		try{
			String head = (String) args[0];
			int index1 = first.indexOf(head);
			if (index1 >= 0) {
				if("after".equals(operator)){
					return first.substring(index1+head.length());
				}else{
					String nstr = operator.substring("after".length());
					int n = Integer.parseInt(nstr);
					return first.substring(index1+head.length(), index1+head.length()+n);
				}
			}
			throw new OperatorException("未在 “"+first+"” 找到位于 “"+head+"” 之后的内容");
		} catch(OperatorException ex){
			throw ex;
		} catch(Exception ex){
			throw new OperatorException("after 用法错误", ex);
		}
	}

}
